package com.company.OfferGuide;

import com.company.ListNode;

public class 链表中倒数第k个节点 {

    public static void main(String[] args) {
        ListNode node = new ListNode(1);
        ListNode kth = getKthFromEnd(node, 1);
        System.out.println(kth.val);
    }

    public static ListNode getKthFromEnd(ListNode head, int k) {
        if (head == null) return null;
        if (k <= 0) return null;
        ListNode front = head,next = head;
        while (front != null && k > 0) {
            front = front.next;
        }
        while (front != null) {
            next = next.next;
            front = front.next;
        }
        return next;
    }

}
